package com.mgg.common.handler;

import com.mgg.common.enums.ResultEnum;
import com.mgg.common.exception.ServiceException;
import com.mgg.common.util.ResultVOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Author: lijun
 * @Date: 2018/9/11 15:49
 */
@ControllerAdvice
public class ServiceExceptionHandler {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 拦截捕捉 Exception.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object handlerException(Exception ex) {
        logger.error(ex.getMessage(),ex);
        return ResultVOUtil.error(ResultEnum.SYSTEM_ERROR);
    }

    /**
     * 拦截捕捉自定义异常 ServiceException.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public Object handlerServiceException(ServiceException ex) {
        logger.error(ex.getMessage(),ex);
        return ResultVOUtil.error(ex.getCode(),ex.getMessage());
    }
}
